<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" v-show="showSearch" :inline="true">
            <el-form-item label="楼宇" prop="buildId">
                <el-select v-model="queryParams.buildId" placeholder="请选择楼宇" @change="changeBuild">
                    <el-option v-for="item in buildings" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="时间范围" prop="dateRange">
                <el-date-picker v-model="queryParams.dateRange" type="monthrange" range-separator="到"
                    start-placeholder="开始时间" end-placeholder="结束时间" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="Plus" size="mini" @click="handleAdd"
                    v-hasRole="['plumber']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="Delete" size="mini" @click="handleDelete"
                    v-hasRole="['plumber']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="Download" size="mini" @click="handleExport"
                    v-hasRole="['plumber']">导出</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <!-- 表格数据 -->
        <el-table v-loading="loading" :data="recordList" stripe border @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" sortable />
            <el-table-column label="时间" prop="time" :show-overflow-tooltip="true" width="80" sortable />
            <el-table-column label="宿舍" prop="dormId" :show-overflow-tooltip="true" width="80" sortable />
            <el-table-column label="用水量(吨)" prop="waterConsum" :show-overflow-tooltip="true" width="120" sortable />
            <el-table-column label="水费(元)" prop="waterCharge" width="100" sortable />
            <el-table-column label="用电量(度)" align="center" prop="electricityConsum" width="120" sortable />
            <el-table-column label="电费(元)" align="center" prop="electricityCharge" width="100" sortable />
            <el-table-column label="缴费" prop="isPay" width="90" sortable>
                <template #default="scope">
                    <el-tag class="ml-2" v-if="scope.row.isPay == 1">
                        已缴费
                    </el-tag>
                    <el-tag class="ml-2" type="danger" v-if="scope.row.isPay == 0">
                        未缴费
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column label="创建时间" align="center" prop="addTime" width="160" sortable />
            <el-table-column label="创建者" align="center" prop="addName" width="120" sortable />
            <el-table-column label="最后修改时间" align="center" prop="modifyTime" width="160" sortable />
            <el-table-column label="修改者" align="center" prop="modifyName" width="120" sortable />
            <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width" width="70">
                <template #default="scope">
                    <el-button size="mini" type="text" icon="Promotion" @click="updateShow(scope.row)"
                        v-hasRole="['plumber']">修改</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />

        <!-- append-to-body -->
        <el-dialog title="修改水电费信息" v-model="open" width="500px">
            <el-form ref="formRef" :model="updateForm" :rules="rules2" label-width="80px">
                <el-form-item label="用水量" prop="waterConSum">
                    <el-input-number v-model="updateForm.waterConsum" :precision="2" :step="0.1" :max="1000" />
                </el-form-item>
                <el-form-item label="水费" prop="waterCharge">
                    <el-input-number v-model="updateForm.waterCharge" :precision="2" :step="0.1" :max="1000" />
                </el-form-item>
                <el-form-item label="用电量" prop="electricityConSum">
                    <el-input-number v-model="updateForm.electricityConsum" :precision="2" :step="0.1" :max="1000" />
                </el-form-item>
                <el-form-item label="电费" prop="electricityCharge">
                    <el-input-number v-model="updateForm.electricityCharge" :precision="2" :step="0.1" :max="1000" />
                </el-form-item>
            </el-form>
            <el-form class="btnGroup">
                <el-button size="large" :loading="updateLoading" type="primary" @click="update">确 定</el-button>
                <el-button size="large" @click="cancel">取 消</el-button>
            </el-form>
        </el-dialog>
    </div>
</template>

<script setup name="plumberFareInfo">
import { reactive } from 'vue';
import { ElNotification } from 'element-plus'
import { getBuildingApi, getWaterElectricityInfoApi, updateWaterElectricityInfoApi, delWaterElectricityRecordApi } from '@/api/plumber'
import { exportExcel } from '@/utils/excelExport'
const router = useRouter();
const { proxy } = getCurrentInstance();

const recordList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const total = ref(0);
const buildings = ref([])
const open = ref(false)

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        buildId: undefined,
        dateRange: undefined
    }
});

const { queryParams, form } = toRefs(data);

const updateForm = ref()

const getBuildings = () => {
    getBuildingApi().then(res => {
        if (res.code == 200) {
            buildings.value = res.data
        }
    })
}
getBuildings()

const getList = () => {
    getWaterElectricityInfoApi(queryParams.value).then(res => {
        console.log(res);
        if (res.code == 200) {
            total.value = res.data.total
            recordList.value = res.data.vo
            loading.value = false
        }
    })
}
getList()

const updateShow = (row) => {
    open.value = true
    updateForm.value = row
}

const update = () => {
    updateWaterElectricityInfoApi(updateForm.value).then(res => {
        if (res.code == 200) {
            ElNotification({
                title: '提示',
                message: '修改成功',
                type: 'success'
            })
            open.value = false
            proxy.resetForm("formRef")
            updateForm.value = ''
        }
    })
}

const cancel = () => {
    updateLoading.value = false
    open.value = false
    proxy.resetForm("formRef")
    updateForm.value = ''
}
/** 多选 删除按钮操作 */
function handleDelete() {
    if (ids && ids.value.length > 0) {
        let id = ""
        console.log(ids.value);
        console.log(ids.value.length);
        proxy.$modal.confirm('是否确认删除选中的数据项?').then(function () {
            if (ids && ids.value.length > 0) {
                for (let i = 0; i < ids.value.length; i++) {
                    if (i <= ids.value.length - 2) {
                        id += ids.value[i] + ","
                    } else if (i == ids.value.length - 1) {
                        id += ids.value[i]
                    }
                }
            }
            return delWaterElectricityRecordApi(id);
        }).then(() => {
            getList();
            ElNotification({
                title: '提示',
                message: '删除成功',
                type: 'success'
            })
        }).catch(() => { });
    } else {
        proxy.$modal.msgWarning("请选择要删除的数据项");
    }
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.id)
}
/** 导出按钮操作 */
function handleExport() {
    let arr = []
    recordList.value.forEach(item => {
        let out = {}
        out.time = item.time;
        out.dormId = item.dormId;
        out.waterConsum = item.waterConsum;
        out.waterCharge = item.waterCharge;
        out.electricityConsum = item.electricityConsum;
        out.electricityCharge = item.electricityCharge;
        if (item.isPay == 1) {
            out.isPay = '已缴费'
        } else {
            out.isPay = '未缴费'
        }
        out.addTime = item.addTime;
        out.addName = item.addName;
        out.modifyTime = item.modifyTime;
        out.modifyName = item.modifyName
        arr.push(out)
    })
    const titleArr = ['时间', '宿舍', '用水量(吨)', '水费(元)', '用电量(度)', '电费(元)', '缴费', '创建时间', '创建者', '最后修改时间', '最后修改者']  //表头中文名
    exportExcel(arr, '水电费信息', titleArr, 'Sheet1')
}
/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1
    getList()
}
/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}
const handleAdd = () => {
    router.push('/fare/add')
}

</script>
